﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using System.IO;

namespace PeisongWeb.admin
{
    public partial class companyAutoPercent : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dptBind();
            }
        }

        public int companyId
        {
            get
            {
                int tmp = 0;
                int.TryParse(ddlCompanyList.SelectedValue,out tmp);
                return tmp;
            }
        }

        private void dptBind()
        {
            DataTable dt=logic.company.listWithCompanyType("2");
            ddlCompanyList.DataSource = dt;
            ddlCompanyList.DataBind();
        }

        protected void ddlCompanyList_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = logic.category.listWithCompanyRange(companyId);
            ddlCategory.DataSource = dt;
            ddlCategory.DataBind();

            ulPanel.Visible = true;
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptBind();
        }

        protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            rptBind();
        }

        private void rptBind()
        {
            StringBuilder where = new StringBuilder();
            where.Append("1=1");
            where.AppendFormat(" and companyId={0}", companyId);

            int categoryId = 0;
            int.TryParse(ddlCategory.SelectedValue, out categoryId);

            if (categoryId != -1)
            {
                where.AppendFormat(" and categoryId in (select categoryId from category where parentId={0})", categoryId);
            }

            DataSet ds = logic.priceStructure.listByCompanyAndCategory(where.ToString(), "sId", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();
        }

        protected void btnExportExcel_Click(object sender,EventArgs e)
        {
            HSSFWorkbook workBook = new HSSFWorkbook();

            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "杭州农副产品物流网络科技有限公司";
            workBook.DocumentSummaryInformation = dsi;

            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Subject = "采购配送系统配送价格单页";
            workBook.SummaryInformation = si;

            HSSFSheet sheet = workBook.CreateSheet("价格单页");

            DataTable dt = logic.product.listWithPriceByBuyer(companyId);

            int rowIndex = 0;
            int colIndex = 0;

            HSSFRow newRow = null;
            HSSFCellStyle style = null;
            HSSFCell cell=null;

            foreach (DataRow row in dt.Rows)
            {
                newRow = sheet.CreateRow(rowIndex++);
                colIndex = 0;
                foreach (DataColumn col in dt.Columns)
                {
                    cell=newRow.CreateCell(colIndex++);
                    cell.SetCellValue(row[col].ToString());
                    if (rowIndex % 2 == 1)
                    {
                        style = workBook.CreateCellStyle();
                        style.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.GREY_80_PERCENT.index;
                        style.FillPattern = HSSFCellStyle.SOLID_FOREGROUND;
                        cell.CellStyle = style;
                    }
                }
            }

            string filename = ddlCompanyList.SelectedItem.Text + ".xls";
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8)));
            Response.Clear();

            MemoryStream memoryStream = new MemoryStream();
            workBook.Write(memoryStream);

            Response.BinaryWrite(memoryStream.GetBuffer());
            Response.End();
        }
    }
}
